package info.niwota.install;

import info.niwota.webapp.InstallHotwebActivity;
import info.niwota.webapp.WebViewActivity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

/**
 * APK Install
 * 
 * Install Android applications from a browser over WiFi.
 * 
 * You've got many choices, here is another one.
 * 
 * This app is intended for Android developers as well as advanced users who are stuck 
 * with Issue# 1039, need to install non Market applications, repeatedly, 
 * or on more than one device.
 * 
 * @author qiangli
 *
 */
public class MainlActivity extends Activity implements OnClickListener {
	private static final String HELP_URI = "file:///android_asset/help.htm";

	private static final String TAG = "MainlActivity";

    private boolean avail;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        
        setContentView(R.layout.main);
        
        setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);
        
        TextView v = (TextView) findViewById(R.id.text);
        avail = InstallHotwebActivity.isHotwebAvailable(this);

        v.setEnabled(true);
    	v.setClickable(true);
    	v.setText(avail ? R.string.hotweb_blackout : R.string.hotweb_install);
    	v.setOnClickListener(this);
    }

	public void onClick(View v) {
		if (v.getId() == R.id.text) {
			if (avail) {
				Screens.blackscreen(this, 1);
			} else {
				InstallHotwebActivity.startActivity(this);
			}
		}
	}
	
	private void doHelp() {
		try {
			Intent i = new Intent(this, WebViewActivity.class);
			i.setData(Uri.parse(HELP_URI));
			startActivity(i);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_option, menu);
		//
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case R.id.main_quit_opt_menu_id:
			doQuit();
			break;
		case R.id.main_settings_opt_menu_id:
			doSettings();
			break;
		case R.id.main_help_opt_menu_id:
			doHelp();
			break;
		default:
			return false;
		}
		return true;
	}

	private void doQuit() {
		if (Debug.DEBUG) {
			Log.d(TAG, "doQuit");
		}
		try {
			finish();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void doSettings() {
		Intent i = new Intent(this, SettingsActivity.class);
		startActivity(i);
	}
}